package com.iakuil.app.common.core.util;

import cn.hutool.core.util.ArrayUtil;
import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数组工具类
 *
 * @author Kai
 */
@UtilityClass
public class ArrayUtils extends ArrayUtil {

    /**
     * 数组分片
     *
     * @param original  数组
     * @param chunkSize 分片大小
     * @return 分片后的数组
     */
    public static <T> List<T[]> partition(T[] original, int chunkSize) {
        List<T[]> result = new ArrayList<>();
        if (original == null) {
            return result;
        }

        int curPos = 0;
        while (curPos < original.length) {
            int remaining = original.length - curPos + 1;
            if (remaining <= chunkSize) {
                result.add(Arrays.copyOfRange(original, curPos, original.length));
            } else {
                result.add(Arrays.copyOfRange(original, curPos, curPos + chunkSize));
            }
            curPos += chunkSize;
        }

        return result;
    }
}
